package com.zengqingfa.algorithm.cz.chapter03.test;

/**
 * @author zengqingfa
 * @className FastSlowTest5
 * @description
 * @create 2021/9/1 19:03
 */
public class FastSlowTest5 {

    public static void main(String[] args) {
        //构件循环链表
        Node<Integer> first = null;
        Node<Integer> pre = null;
        for (int i = 1; i <= 41; i++) {
            if (i == 1) {
                first = new Node<Integer>(i, null);
                pre = first;
                continue;
            }
            Node<Integer> node = new Node<>(i, null);
            pre.next = node;
            pre = node;
            if (i == 41) {
                pre.next = first;
            }
        }

        //使用count记录当前报的值
        int count = 0;
        //遍历链表
        Node<Integer> n = first;
        Node<Integer> before = null;
        while (n != n.next) {
            //判断count的值，如果是3，则从链表中删除这个结点并打印结点的值，把count重置为0；
            count++;
            if (count == 3) {
                //删除当前节点
                before.next = n.next;
                System.out.println(n.item + "");
                count = 0;

            } else {
                before = n;
            }
            n = n.next;
        }
        //最后一个人
        System.out.println(n.item);

    }


    /**
     * 静态内部类 节点
     *
     * @param <T>
     */
    private static class Node<T> {
        private T item;
        private Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}